package com.xinruke.hostels.common.util;

import com.google.common.collect.Maps;
import com.xinruke.hostels.common.vo.WeightCategory;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @Author: chenry
 * @Date: 2018/5/9 10:27
 * @Description: 随机数据通用工具类
 */
public class RandomUtil {
    private final static String LONG_STR = "1234567890";

    private final static String sms_default_template = "SMS_165360547";
    private static final Random random = new Random();

//    public static List<WeightCategory>  categorys = new ArrayList<WeightCategory>();
//    static {
//        WeightCategory wc1 = new WeightCategory("A",70);
//        WeightCategory wc2 = new WeightCategory("B",40);
//        WeightCategory wc3 = new WeightCategory("C",30);
//        WeightCategory wc4 = new WeightCategory("D",40);
//
//        categorys.add(wc2);
//        categorys.add(wc1);
//        categorys.add(wc3);
//        categorys.add(wc4);
//    }

//    /**
//     * 获得给定List集合里权重大的结果
//     * @return
//     */
//    public static synchronized String getMax(){
//        Integer weightSum = 0;
//        for (WeightCategory wc : categorys) {
//            weightSum += wc.getWeight();
//        }
//
//        if (weightSum <= 0) {
//            System.err.println("Error: weightSum=" + weightSum.toString());
//            return "";
//        }
//        Integer n = random.nextInt(weightSum); // n in [0, weightSum)
//        Integer m = 0;
//        String category="";
//        for (WeightCategory wc : categorys) {
//            if (m <= n && n < m + wc.getWeight()) {
//                category = wc.getCategory();
//                //System.out.println("This Random Category is "+wc.getCategory());
//                break;
//            }
//            m += wc.getWeight();
//        }
//        return category;
//    }

    /**
     * 获得给定List集合里权重大的结果
     *
     * @param categorys
     * @param weightSum
     * @return
     */
    public static synchronized String getMax(List<WeightCategory> categorys, int weightSum) {
        int n = random.nextInt(weightSum); // n in [0, weightSum)

        int m = 0;
        String category = "";
        for (WeightCategory wc : categorys) {
            if (m <= n && n < m + wc.getWeight()) {
                category = wc.getCategory();
                //System.out.println("This Random Category is "+wc.getCategory());
                break;
            }
            m += wc.getWeight();
        }
        return category;
    }


    /**
     * 随机[0,1]数据
     *
     * @return
     */
    public static synchronized int getRandomZeroOrOne() {
        int num = random.nextInt(2);
        return num;
    }

    /**
     * 随机数组集合里面的数据
     *
     * @param dataArray
     * @return
     */
    public static synchronized int getRandomByDataArray(int[] dataArray) {
        int num = dataArray.length;
        int index = random.nextInt(num);
        return dataArray[index];
    }

    /**
     * 随机范围数据里面的数,其中包含min,max
     *
     * @param min
     * @param max
     * @return
     */
    public static synchronized int getRandomByRange(int min, int max) {
        int num = random.nextInt(max - min + 1) + min;
        return num;
    }


    public static String createCode(int length) {
        String retStr = "";
        int len = LONG_STR.length();
        boolean bDone = true;
        do {
            retStr = "";
            int count = 0;
            for (int i = 0; i < length; i++) {
                double dblR = Math.random() * len;
                int intR = (int) Math.floor(dblR);
                char c = LONG_STR.charAt(intR);
                if (('0' <= c) && (c <= '9')) {
                    count++;
                }
                retStr += LONG_STR.charAt(intR);
            }
            if (count >= 2) {
                bDone = false;
            }
        } while (bDone);
        return retStr;
    }


    public static void main(String[] args) {
        Map<Integer, Integer> dataMap = Maps.newConcurrentMap();

       /* Map<String, Integer> map = new HashMap<String, Integer>();
        String str = null;
        Long s = System.currentTimeMillis();

        int[] oneData = new int[]{1001, 1003, 1006, 1009, 1011, 1022};

        for (int i = 0; i < 10000000; i++) {
            //int randomNum = RandomUtil.getRandomByRange(0,2);

            str = RandomUtil.getRandomByRange(0,2) + "";
            //System.out.println("===="+str);

            //str= getRandomByRange(50,150) + "";

            //str= getRandomByRange(50,150) + "";

            if (map.containsKey(str)) {
                map.put(str, (map.get(str) + 1));
            } else {
                map.put(str, 1);
            }
        }

//        for(int i=0;i<100000;i++){
//            str = getMax();
//            if(map.containsKey(str)){
//                map.put(str,(map.get(str) +1));
//            }else{
//                map.put(str,1);
//            }
//        }
        Long e = System.currentTimeMillis();
        System.out.println("耗时：" + (e - s));
        for (Map.Entry entry : map.entrySet()) {
            System.out.println("key=" + entry.getKey() + " count=" + entry.getValue() + " 概率=" + (new BigDecimal(entry.getValue().toString()).divide(new BigDecimal(10000000))));
        }*/

    }

}  
   
